<?php

namespace app\common\model;

use think\Cache;

/**
 * 教学课程练习模型
 * Class ZhunleCourseSectionExercise
 * @package app\common\model
 */
class ZhunleCourseSectionExercise extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.zhunle_course_section_exercise';

    const COURSE_SECTION_EXERCISE_CISE = 'course:section:exercise:csid';// 章节练习ID
    const CACHE_TIME = 86400 * 7;

    /**
     * 通过章节ID 获取练习信息
     * @param $csid
     * @return array|bool|mixed
     */
    public function getSectionExercise($csid)
    {
        $sectionExerciseData = false;

        if ($this->cache_on === 1)
        {
            $sectionExerciseData = Cache::get(self::COURSE_SECTION_EXERCISE_CISE . $csid);
        }
        if ($sectionExerciseData === false)
        {
            try
            {
                // 获取数据
                $sectionExerciseDataModel = $this->where(['csid' => $csid, 'status' => 1])
                    ->field('title,title_img,optionA,optionB,optionC,optionD,answer,analysis')
                    ->selectOrFail();

                $sectionExerciseData = json_encode($sectionExerciseDataModel, true);
            }
            catch (\Exception $e)
            {
                $sectionExerciseData = '[]';
            }
            if ($this->cache_on)
            {
                Cache::set(self::COURSE_SECTION_EXERCISE_CISE . $csid, $sectionExerciseData, self::CACHE_TIME);
            }
        }
        return $sectionExerciseData;
    }
}
